﻿using System;
using System.ServiceModel;

public class DistributedComputingServiceClient
{

    public static ChannelFactory<ISvc> CreateWebChannelFactory<ISvc>(string RemoteAddress, string ServiceName)
    {
        BasicHttpBinding theBinding = new BasicHttpBinding();
        theBinding.MaxReceivedMessageSize = int.MaxValue;
        theBinding.MaxBufferSize = int.MaxValue;
        theBinding.MaxBufferPoolSize = int.MaxValue;
        theBinding.ReaderQuotas.MaxDepth = 32;
        theBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
        theBinding.ReaderQuotas.MaxArrayLength = 2147483647;
        theBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
        theBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        theBinding.Security.Mode = BasicHttpSecurityMode.None;
        theBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

        return new ChannelFactory<ISvc>(theBinding, RemoteAddress);

    }

    /// <summary>
    /// 传Stream用的
    /// </summary>
    /// <typeparam name="ISvc"></typeparam>
    /// <param name="RemoteAddress"></param>
    /// <param name="ServiceName"></param>
    /// <returns></returns>
    public static ChannelFactory<ISvc> CreateStreamChannelFactory<ISvc>(string RemoteAddress, string ServiceName)
    {
        BasicHttpBinding theBinding = new BasicHttpBinding();
        theBinding.MaxReceivedMessageSize = int.MaxValue;
        theBinding.MaxBufferSize = int.MaxValue;
        theBinding.MaxBufferPoolSize = int.MaxValue;
        theBinding.ReaderQuotas.MaxDepth = 32;
        theBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
        theBinding.ReaderQuotas.MaxArrayLength = 2147483647;
        theBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
        theBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        theBinding.Security.Mode = BasicHttpSecurityMode.None;
        theBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        theBinding.TransferMode = TransferMode.Streamed;
        theBinding.MessageEncoding = WSMessageEncoding.Mtom;

        return new ChannelFactory<ISvc>(theBinding, RemoteAddress);

    }

    /// <summary>
    /// net.tcp
    /// </summary>
    /// <typeparam name="ISvc"></typeparam>
    /// <param name="RemoteAddress"></param>
    /// <param name="ServiceName"></param>
    /// <returns></returns>
    public static ChannelFactory<ISvc> CreateTcpChannelFactory<ISvc>(string RemoteAddress, string ServiceName)
    {
        NetTcpBinding theBinding = new NetTcpBinding();
        theBinding.MaxReceivedMessageSize = int.MaxValue;
        theBinding.MaxBufferSize = int.MaxValue;
        theBinding.MaxBufferPoolSize = int.MaxValue;
        theBinding.ReaderQuotas.MaxDepth = 32;
        theBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
        theBinding.ReaderQuotas.MaxArrayLength = 2147483647;
        theBinding.ReaderQuotas.MaxBytesPerRead = 4096;
        theBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        theBinding.Security.Mode = SecurityMode.None;
        //theBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
        //theBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
        theBinding.TransferMode = TransferMode.Buffered;
        theBinding.TransactionProtocol = TransactionProtocol.OleTransactions;
        theBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        theBinding.MaxConnections = 10;
        theBinding.TransactionFlow = false;
        theBinding.CloseTimeout = TimeSpan.MaxValue;
        theBinding.SendTimeout = TimeSpan.MaxValue;
        return new ChannelFactory<ISvc>(theBinding, RemoteAddress);


        //TcpTransportBindingElement transport = new TcpTransportBindingElement();
        //transport.TransferMode = TransferMode.Streamed;
        //BinaryMessageEncodingBindingElement encoder = new BinaryMessageEncodingBindingElement();
        //CustomBinding cb = new CustomBinding(transport, encoder);
        //return new ChannelFactory<ISvc>(cb, RemoteAddress);



    }
}
